package com.example.lyl.yuejian.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.adapter.ClubLvAdapter;
import com.example.lyl.yuejian.bean.ClubLvBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.view.club.ClubDetialActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubFragment extends Fragment {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    Unbinder unbinder;
    @BindView(R.id.club_lv)
    ListView clubLv;

    ClubLvBean clubLvBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_club, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        toolbar.setTitle("俱乐部");
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        final ClubLvAdapter adapter = new ClubLvAdapter();
        clubLv.setAdapter(adapter);

        NetTool.getInstance().getApi()
                .clubListRequest()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ClubLvBean>() {
                    @Override
                    public void accept(ClubLvBean clubLvBean) throws Exception {
                        ClubFragment.this.clubLvBean = clubLvBean;
                        adapter.setClubLvBean(clubLvBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(getContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    }
                });

            clubLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ClubDetialActivity.class);
                    intent.putExtra("cid",clubLvBean.getData().get(position).getCid());
                    startActivity(intent);
                }
            });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
